package com.ynet.eq.common.exception;
import lombok.Data;
import static com.ynet.eq.common.exception.ResponseCodeEnum.SUCCEED_CODE;

/**
 * @author 胡超雄
 * @date 2021/7/8 14:13
 * @desc  返回统一对象
 */
@Data
public class ResponseEntity <T> {

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 说明
     */
    private String msg;

    /**
     * 相关信息
     */
    private T data;

    /**
     * 信息
     */
    private Long expired_at;


    public static <T> ResponseEntity<T> ok() {
        return restResult(null, SUCCEED_CODE, null);
    }

    public static <T> ResponseEntity<T> ok(String msg) {
        return restResult(null, SUCCEED_CODE, msg);
    }

    public static <T> ResponseEntity<T> ok(T data) {
        return restResult(data, SUCCEED_CODE, null);
    }

    public static <T> ResponseEntity<T> ok(T data, String msg) {
        return restResult(data, SUCCEED_CODE, msg);
    }

    public static <T> ResponseEntity<T> response(String code, String msg, T data) {
        return restResult(data, Integer.parseInt(code), msg);
    }


    public static <T> ResponseEntity<T> response(ResponseCodeEnum responseCode) {
        return restResult(null, responseCode, null);
    }

    public static <T> ResponseEntity<T> response(ResponseCodeEnum responseCode, String msg) {
        return restResult(null, responseCode, msg);
    }

    public static <T> ResponseEntity<T> response(ResponseCodeEnum responseCode, T data) {
        return restResult(data, responseCode, null);
    }

    public static <T> ResponseEntity<T> response(ResponseCodeEnum responseCode, T data, String msg) {
        return restResult(data, responseCode, msg);
    }

    private static <T> ResponseEntity<T> restResult(T data, ResponseCodeEnum responseCode, String inputMsg) {
        ResponseEntity<T> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(responseCode.getCode());
        responseEntity.setData(data);
        if (inputMsg == null) {
            responseEntity.setMsg(responseCode.getMsg());
        } else {
            responseEntity.setMsg(inputMsg);
        }
        responseEntity.setExpired_at(System.currentTimeMillis());
        return responseEntity;
    }

    private static <T> ResponseEntity<T> restResult(T data, Integer code, String msg) {
        ResponseEntity<T> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(code);
        responseEntity.setData(data);
        responseEntity.setMsg(msg);
        responseEntity.setExpired_at(System.currentTimeMillis());
        return responseEntity;
    }
}
